package web.controller.employer;

import common.consts.ContentType;
import common.validation.ValidationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import service.EmployService;
import service.ServiceResponse;
import web.Routes;
import web.controller.Base;
import web.req.EmployRequest;
import web.security.Protection;

/**
 * Created by aceyin on 15/6/30.
 * 录用
 */
@RestController
@RequestMapping(Routes.Employer.EMPLOY)
public class Employ extends Base {

    @Autowired
    private EmployService employService;

    @Protection
    @RequestMapping(method = RequestMethod.POST, produces = ContentType.JSON)
    public ServiceResponse employ(@RequestBody EmployRequest request) {
        ValidationResult result = request.validate();
        if (result.isFailed()) {
            fail(result.getMessage());
        }

        long employer = getUserIdFromAuthHeader();

        long recruitment = request.getRecruitment();
        long applier = request.getApplier();

        ServiceResponse response;
        if (applier <= 0) {
            response = employService.employAll(employer, recruitment);
        } else {
            response = employService.employ(employer, applier, recruitment);
        }
        return response;
    }
}
